#%%
import sys
from loguru import logger
import os
import traceback
# 多线程的logger https://www.bilibili.com/video/BV17h4y1S7cx/?p=10&spm_id_from=pageDriver 
# https://loguru.readthedocs.io/en/stable/resources/recipes.html#compatibility-with-multiprocessing-using-enqueue-argument

class Logger:
    
    @staticmethod
    def init_logger(self):
        global log_abs_path
        log_abs_path = "./"
        # 存储info及以上级别的日志
        logger.add(
            os.path.join(log_abs_path, "info.log"), 
            format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}", 
            level="INFO" , 
            enqueue=True,
            encoding='utf-8',
            backtrace=True,
            diagnose=True,
            retention="7 days")
        
        logger.add(
            os.path.join(log_abs_path, "error.log"), 
            format="{time:YYYY-MM-DD HH:mm:ss} | {level} | {message}", 
            level="ERROR", 
            enqueue=True,
            encoding='utf-8',
            backtrace=True,
            diagnose=True,
            retention="7 days")
        
        sys.excepthook = self.handle_exception
    
    @staticmethod
    def handle_exception(exc_type, exc_value, exc_traceback):
        # if issubclass(exc_type, KeyboardInterrupt):
        #     sys.__excepthook__(exc_type, exc_value, exc_traceback)
        #     return
        # logger.error("Uncaught exception", exc_info=(exc_type, exc_value, exc_traceback))

        logger.error("Uncaught exception", ''.join(traceback.format_exception(exc_type, exc_value, exc_traceback)))

    @staticmethod
    def i(message):
        logger.info(message)
    
    @staticmethod
    def e(message):
        logger.error(message)
    
    @staticmethod
    def w(message):
        logger.warning(message)
    
    @staticmethod
    def d(message):
        logger.debug(message)
    
# %%
# logger = Logger()
# logger.init_logger()
Logger.i("This is an info message")
Logger.e("This is an error message")
Logger.w("This is a warning message")
Logger.d("This is a debug message")
# #  %%

a = 1/0


# %%
